package com.yuyou.fn.portal.service.impl;

import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.portal.service.IOuterCourseClassService;
import com.yuyou.fn.portal.service.IOuterTeacherService;
import com.yuyou.fn.portal.vo.OuterCourseClassQueryVo;
import com.yuyou.fn.portal.vo.OuterTeacher;
import com.yuyou.fn.portal.vo.OuterTeacherQueryVo;
import com.yuyou.fn.portal.vo.SimpleCourseClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by wens on 2017/4/6.
 */
@Service
public class OuterTeacherServiceImpl implements IOuterTeacherService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private IOuterCourseClassService outerCourseClassService;


    @Override
    public List<OuterTeacher> findTeacherByCond(int pageNo, int pageSize, OuterTeacher outerTeacher) {
        if (pageNo>0) pageNo=pageNo-1;
        Query query=new Query();
        if(StringUtils.isNotEmpty(outerTeacher.getCity())){
            query.addCriteria(Criteria.where("city").is(outerTeacher.getCity()));
        }
        if(StringUtils.isNotEmpty(outerTeacher.getTeacherName()) && StringUtils.isNotEmpty(outerTeacher.getTeacherName().trim())){
            query.addCriteria(Criteria.where("teacherName").regex(".*?" +outerTeacher.getTeacherName().trim()+ ".*"));
        }

        if (StringUtils.isNotEmpty(outerTeacher.getTeachGradeIds()) && !"10000".equals(outerTeacher.getTeachGradeIds())){
            query.addCriteria(Criteria.where("teachGradeIds").regex(".*?" + outerTeacher.getTeachGradeIds() + ".*"));
        }
        if (StringUtils.isNotEmpty(outerTeacher.getTeachSubjectIds()) && !"10000".equals(outerTeacher.getTeachSubjectIds())){
            query.addCriteria(Criteria.where("teachSubjectIds").regex(".*?" + outerTeacher.getTeachSubjectIds() + ".*"));
        }
      /*  if (StringUtils.isNotEmpty(outerTeacher.getJobNature())){
            query.addCriteria(Criteria.where("jobNature").is(outerTeacher.getJobNature()));
        }*/
        query.addCriteria(Criteria.where("jobNature").ne("1"));/*去除外部兼职的老师*/
        query.addCriteria(Criteria.where("status").is(1));
        query.addCriteria(Criteria.where("avatarUrl").ne("").where("avatarUrl").ne(null));
        query.addCriteria(Criteria.where("accessType").is(0));
        List<OuterTeacher> outerTeacherList=mongoTemplate.find(query.with(new Sort(new Sort.Order(Sort.Direction.DESC,"createTime"))).skip(pageNo*pageSize).limit(pageSize),OuterTeacher.class);
        for (OuterTeacher outerTeacher1:outerTeacherList){
            OuterCourseClassQueryVo outerCourseClassQueryVo=new OuterCourseClassQueryVo();
            outerCourseClassQueryVo.setTeacherId(outerTeacher1.getTeacherId());
            //outerCourseClassQueryVo.setCity(outerTeacher.getCity());
            List<SimpleCourseClass> courseClassResultVoList=outerCourseClassService.findOuterCourseClassByCondition(outerCourseClassQueryVo,-1,-1);
            outerTeacher1.setCourseClassResultVoList(courseClassResultVoList);
        }
        return   outerTeacherList;//mongoTemplate.find(query.with(new Sort(new Sort.Order(Sort.Direction.DESC,"createTime"))).skip(pageNo*pageSize).limit(pageSize),OuterTeacher.class);
    }

    @Override
    public OuterTeacher findTeacherDetail(Long teacherId) {
        Criteria criteria = new Criteria();
        Query query=new Query();
        query.addCriteria(criteria.where("teacherId").is(teacherId));
        OuterTeacher outerTeacher=mongoTemplate.findOne(query,OuterTeacher.class);
        return   outerTeacher;
    }


    @Override
    public Page<OuterTeacher> findTeacher(OuterTeacherQueryVo outerTeacherQueryVo, int pageNo, int pageSize) {
        if (pageNo>0) pageNo=pageNo-1;
        Query query=new Query();
        if(outerTeacherQueryVo.getAreaTeamId()!=null){
            query.addCriteria(Criteria.where("areaTeamId").is(outerTeacherQueryVo.getAreaTeamId()));
        }
        if(StringUtils.isNoneEmpty(outerTeacherQueryVo.getTeacherName())){
            query.addCriteria(Criteria.where("teacherName").regex(".*?" +outerTeacherQueryVo.getTeacherName().trim()+ ".*"));
        }

        if (outerTeacherQueryVo.getGradeId() != null ){
            query.addCriteria(Criteria.where("teachGradeIds").regex(".*?" + outerTeacherQueryVo.getGradeId() + ".*"));
        }

        if(StringUtils.isNoneEmpty(outerTeacherQueryVo.getGradeName())){
            query.addCriteria(Criteria.where("teachGradeNames").regex(".*?" + outerTeacherQueryVo.getGradeName()+ ".*"));
        }

        if ( outerTeacherQueryVo.getSubjectId() != null ){
            query.addCriteria(Criteria.where("teachSubjectIds").regex(".*?" + outerTeacherQueryVo.getSubjectId() + ".*"));
        }

        if(StringUtils.isNoneEmpty(outerTeacherQueryVo.getSubjectName())){
            query.addCriteria(Criteria.where("teachSubjectNames").regex(".*?" + outerTeacherQueryVo.getSubjectName() + ".*"));
        }

        if(StringUtils.isNoneEmpty(outerTeacherQueryVo.getKeyword())){
            query.addCriteria(Criteria.where("teacherName").regex(".*?" + outerTeacherQueryVo.getKeyword() + ".*"));
        }
        query.addCriteria(Criteria.where("jobNature").ne("1"));/*去除外部兼职的老师*/
        query.addCriteria(Criteria.where("status").is(1));
        query.addCriteria(Criteria.where("avatarUrl").ne("").where("avatarUrl").ne(null));
        query.addCriteria(Criteria.where("accessType").is(0));

        long total = mongoTemplate.count(query,OuterTeacher.class);
        List<OuterTeacher> outerTeacherList=mongoTemplate.find(query.with(new Sort(new Sort.Order(Sort.Direction.DESC,"createTime"))).skip(pageNo*pageSize).limit(pageSize),OuterTeacher.class);

        Page<OuterTeacher> page = new Page<>(pageNo,pageSize);
        page.setRecords(outerTeacherList);
        page.setTotal((int)total);
        return page;
    }
}
